﻿function UpLoadImageObj(pick_id, isMultiple) {
    
    var singleImageUpload = WebUploader.create({
        swf: '/webuploader/Uploader.swf',
        fileVal: 'file',
        duplicate: true,
        server: '',        
        pick: { id: '#' + pick_id, multiple: isMultiple },
        accept: {
            title: 'Images',
            extensions: 'jpg,jpeg,png',
            mimeTypes: 'image/jpg,image/jpeg,image/png'
        },
        compress: false,
        method: 'POST',
        threads: 1,
        resize: false,
        chunked: false,
        auto: true,
        ossSignatureUrl: ossSignatureUrl,
        dir: "",
        host: ""
    });
    singleImageUpload.on('beforeFileQueued', function (file) {
        $.ajaxSettings.async = false;
        if (!isMultiple&&file.name.length > 30) {
            layer.msg("上传的文件的文件名过长,请修改本地文件的文件名,确保文件名长度控制在25个字符内", { time: 3000 });
            return false;
        }
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var lastItem = images_list.find(".item").last();
        var lastContent = lastItem.prop("outerHTML");
        if (isMultiple) {
            images_list.append(lastContent);
            lastItem = images_list.find(".item").last();
            lastItem.find("img.myImg").prop("src", "/img/loading.gif");
            lastItem.find("input").val("");
        }
        lastItem.addClass(file.id);       
        

    });
    singleImageUpload.on('uploadProgress', function (file, percentage) {
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var myItem = images_list.find("." + file.id);
        myItem.find(".progress").css("display", "block");
        myItem.find(".progress").find(".progress-bar").css("width", percentage * 100 + "% ");
    });
    singleImageUpload.on('uploadSuccess', function (file, response) {
        if (response != null && response != "" && response._raw.indexOf("<Message>Error") != -1) {
            layer.msg("上传失败!");
            return false;
        }
        var imageValue = response.data[0];
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var myItem = images_list.find("." + file.id);
        myItem.css({ "display": "inline-block","marginRight":"10px"});
        myItem.find("img.myImg").prop("src", imageValue);
        myItem.find("input").val(imageValue);
        myItem.find(".progress").find(".progress-bar").css("display", "none");

    });
    singleImageUpload.on('uploadError', function (file) {
        alert("上传失败!");
    });
}

function RichTextUpLoadImage(pick_id) {
    var singleImageUpload = WebUploader.create({
        swf: '/webuploader/Uploader.swf',
        fileVal: 'file',
        duplicate: true,
        server: '',       
        pick: { id: '.' + pick_id, multiple: false },
        accept: {
            title: 'Images',
            extensions: 'jpg,jpeg,png',
            mimeTypes: 'image/jpg,image/jpeg,image/png'
        },
        compress: false,
        method: 'POST',
        threads: 1,
        resize: false,
        chunked: false,
        auto: true,
        ossSignatureUrl: ossSignatureUrl,
        dir: "",
        host: ""
    });
    singleImageUpload.on('beforeFileQueued', function (file) {        
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var lastItem = images_list.find(".item").last();
        var lastContent = lastItem.prop("outerHTML");
        lastItem.addClass(file.id);
        if (isMultiple) {
            images_list.append(lastContent);
            lastItem = images_list.find(".item").last();
            lastItem.find("img").prop("src", "");
            lastItem.find("input").val("");
        }

    });
    singleImageUpload.on('uploadProgress', function (file, percentage) {
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var myItem = images_list.find("." + file.id);
        myItem.find(".progress").css("display", "block");
        myItem.find(".progress").find(".progress-bar").css("width", percentage * 100 + "% ");
    });
    singleImageUpload.on('uploadSuccess', function (file, response) {
        if (response != null && response != "" && response._raw.indexOf("<Message>Error") != -1) {
            layer.msg("上传失败!");
            return false;
        }
        var ext = file.source.ext.toLowerCase();
        var imageValue = response.data[0];
        var event_id = file.source.ruid;
        var images_list = $("#rt_" + event_id).parent().parent().find(".oss_images_list");
        var myItem = images_list.find("." + file.id);
        myItem.css("display", "block");
        myItem.find("img").prop("src", imageValue);
        myItem.find("input").val(imageValue);
        myItem.find(".progress").find(".progress-bar").css("display", "none");

    });
    singleImageUpload.on('uploadError', function (file) {
        alert("上传失败!");
    });
}

function UpLoadFlieObj(obj_name, pick_id, isMultiple) {
    var singleImageUpload = WebUploader.create({
        swf: '/webuploader/Uploader.swf',
        fileVal: 'file',
        duplicate: true,        
        compress: false,
        pick: { id: '#' + pick_id, multiple: isMultiple },
        method: 'POST',
        threads: 1,
        resize: false,
        chunked: false,
        auto: true,
        ossSignatureUrl: ossSignatureUrl,
        dir: "",
        host: "",
        isMultiple: isMultiple
    });
    singleImageUpload.on('beforeFileQueued', function (file) {
        if (singleImageUpload.options.isMultiple) {
            var event_id = file.source.ruid;
            var images_list = $("#rt_" + event_id).parent().parent().find(".layui-table");
            var mySize = parseInt(file.size / 1024);
            images_list.find("tbody").append("<tr id=\"" + file.id + "\"><td>" + file.name + "</td><td class='mysize'>0KB /" + mySize + "KB</td><td><div class=\"layui-progress layui-progress-big\" lay-showpercent=\"true\" lay-filter=\"" + file.id + "\"><div class=\"layui-progress-bar\" lay-percent=\"0%\" ></div></div></td><td><input type='hidden' name='" + obj_name+"' value='' class='Attachment'/><span class='deleteAttachment'>删除</span></td></tr>");
        }
    });
    singleImageUpload.on('uploadProgress', function (file, percentage) {
        var event_id = file.source.ruid;
        var mySize = parseInt(file.size / 1024);
        var uploadSize = parseInt(file.size * percentage / 1024);
        var images_list = $("#rt_" + event_id).parent().parent().find(".layui-table");
        var myItem = images_list.find("#" + file.id);
        myItem.find(".mysize").html(uploadSize + "KB / " + mySize + "KB");
        element.progress(file.id, percentage * 100 + "% ");

    });
    singleImageUpload.on('uploadSuccess', function (file, response) {       
        if (response!=null&&response!=""&&response._raw.indexOf("<Message>Error") != -1) {
            layer.msg("上传失败!");
            return false;
        }        
        var mySize = parseInt(file.size / 1024);
        var ext = file.source.ext.toLowerCase();
        var imageValue = response.data[0];
        var event_id = file.source.ruid;
        if (singleImageUpload.options.isMultiple) {
            var images_list = $("#rt_" + event_id).parent().parent().find(".layui-table");
            var myItem = images_list.find("#" + file.id);
            element.progress(file.id, "100% ");
            myItem.find(".mysize").html(mySize + "KB / " + mySize + "KB");
            myItem.find(".Attachment").val(imageValue);
        }
        else {
            var input_div = $("#rt_" + event_id).parent().parent().find(".input_div");
            input_div.find("input").val(imageValue);
        }

    });
    singleImageUpload.on('uploadError', function (file) {
        layer.msg("上传失败!");
    });
}

function MyAjax(url) {
    $.getJSON(url, function (data) {
        if (data.State == 200) {
            window.location.href = window.location.href;
        }
        else {
            alert(data.Message);
        }
    });
}


function showWindow(title, url) {
    parent.layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: false,
        maxmin: true,
        area: ['893px', '600px'],
        content: url
    });
}

$(document).on("click", ".oss_images_list .delete", function () {
    if ($(this).parent().parent().parent().find(".item").length === 1) {
        $(this).parent().parent().find("input").val("");
        $(this).parent().parent().find(".myImg").prop("src", "../../img/upload.png");
    }
    else {
        $(this).parent().parent().remove();
    }
});

 
$(document).on("click", ".deleteAttachment", function () {
    $(this).parent().parent().remove();
});